package com.grf.hadoop.practice;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 气温实体类
 */
public class TempBean implements WritableComparable<TempBean> {

    private String year; //年份
    private Double temp; //气温

    public TempBean(){
        super();
    }

    public TempBean(String year, Double temp) {
        this.year = year;
        this.temp = temp;
    }

    @Override
    public int compareTo(TempBean o) {
        int res;
        if (this.year.compareTo(o.getYear()) > 0) {
            res = 1;
        } else if (this.year.compareTo(o.getYear()) < 0) {
            res = -1;
        } else {
            if (this.temp > o.getTemp()) {
                res = -1;
            } else if (this.temp < o.getTemp()) {
                res = 1;
            } else {
                res = 0;
            }
        }
        return res;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(year);
        dataOutput.writeDouble(temp);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        year = dataInput.readUTF();
        temp = dataInput.readDouble();
    }


    public Double getTemp() {
        return temp;
    }

    public void setTemp(Double temp) {
        this.temp = temp;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return year + " " + temp;
    }
}
